package com.bocai.kelalacar.widget;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.CarDriverBean;
import com.bocai.kelalacar.ui.car.main.ada.SpinnerCarAdapter;
import com.bocai.kelalacar.ui.car.main.ada.SpinnerDriverAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yuanfei on 2017/6/21.
 */

public class SpinnerDialog {

    private BaseActivity mContext;
    private ListView mListView;
    private SpinnerCarAdapter mAdapter;
    private SpinnerDriverAdapter mAdapter1;
    private LinearLayout lLayout_bg;
    private View menuView;
    private Display display;
    private Dialog dialog;


    public SpinnerDialog(BaseActivity context)
    {
        this.mContext = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public SpinnerDialog builder() {
        menuView = LayoutInflater.from(mContext).inflate(R.layout.spiner_window_layout, null);
        mListView = (ListView) menuView.findViewById(R.id.listview);
        lLayout_bg = (LinearLayout) menuView.findViewById(R.id.lLayout_bg);
        // 定义Dialog布局和参数
        dialog = new Dialog(mContext, R.style.AlertDialogStyle);
        dialog.setContentView(menuView);

        // 调整dialog背景大小
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.85), LinearLayout.LayoutParams.WRAP_CONTENT));

        return this;
    }
    public SpinnerDialog setCarList(final List<CarDriverBean.ContentBean.CarListBean> list, final AdapterView.OnItemClickListener onItemClickListener){
        mAdapter = new SpinnerCarAdapter(mContext,list);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("onItemClick",position+"");
                    onItemClickListener.onItemClick(parent,view,position,id);
                dismiss();
            }
        });
        return this;
    }

    public SpinnerDialog setDriverList(List<CarDriverBean.ContentBean.DriverListBean> list, final AdapterView.OnItemClickListener onItemClickListener){
        mAdapter1 = new SpinnerDriverAdapter(mContext,list);
        mListView.setAdapter(mAdapter1);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onItemClickListener.onItemClick(parent,view,position,id);
                dismiss();
            }
        });
        return this;
    }
    public void show() {
        dialog.show();
    }
    public void dismiss(){
        dialog.dismiss();
    }

}
